﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;
using System.Text.RegularExpressions;

namespace LTJ.WebPage.UserManage.user
{
    /// <summary>
    /// 企业用户分配
    /// </summary>
    public partial class corpuserdist : UserPage
    {
        /// <summary>
        /// 管理员ID
        /// </summary>
        protected int adminid = SASRequest.GetInt("aid", 0);
        protected int groupid = 0;
        protected string parmStr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            string strWhere = " scid = " + companyinfo.id;

            if (ViewState["condition"] == null || ViewState["condition"].ToString() == "")
            {
                if (parmStr != "") strWhere += " AND " + parmStr;
            }
            else
            {
                if (parmStr != "") strWhere += " AND " + ViewState["condition"].ToString() + " AND " + parmStr;
                else strWhere += " AND " + ViewState["condition"].ToString();
            }

            DataGrid1.VirtualItemCount = Users.GetUsersCount(strWhere);
            DataGrid1.DataSource = Users.GetUsersByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, strWhere, "");
            DataGrid1.DataBind();
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void GoToPagerButton_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 搜索点击
        /// </summary>
        protected void check_Click(object sender, EventArgs e)
        {
            ViewState["condition"] = "";
            string lname = lname_txt.Text;
            string rname = rname_txt.Text;
            string phone = phone_txt.Text;
            int cur_status = TypeConverter.StrToInt(cur_status_txt.SelectedValue, -1);

            string[] constrarray = new string[4];

            if (lname != "" && Utils.IsSafeSqlString(lname)) constrarray[0] = "lname like '%" + lname + "%'";
            else constrarray[0] = "";

            if (rname != "" && Utils.IsSafeSqlString(rname)) constrarray[1] = "rname like '%" + rname + "%'";
            else constrarray[1] = "";

            if (phone != "" && Utils.IsSafeSqlString(phone)) constrarray[2] = "phone like '%" + phone + "%'";
            else constrarray[2] = "";

            if (cur_status > -1) constrarray[3] = "status = " + cur_status;
            else constrarray[3] = "";

            for (int i = 0; i < 4; i++)
            {
                if (String.IsNullOrEmpty(constrarray[i])) continue;

                if (ViewState["condition"] != null && ViewState["condition"].ToString() != "")
                    ViewState["condition"] += " AND " + constrarray[i];
                else
                    ViewState["condition"] = constrarray[i];
            }

            DataGrid1.CurrentPageIndex = 0;
            BindDataGrid();
        }

        /// <summary>
        /// 重新绑定
        /// </summary>
        protected void reclick_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 密码重置
        /// </summary>
        protected void reuserpass_ServerClick(object sender, EventArgs e)
        {
            int ruserid = SASRequest.GetInt("ruserid", 0);
            if (ruserid > 0)
            {
                Users.UpdateUserFid(ruserid, 0);
                base.RegisterStartupScript("", "<script>alert('解除成功!');AjaxHelper.Updater('ajaxcorpuserlist_list.ascx', 'ajaxcorpuserdatalist', 'noinfid=" + adminid + "');</script>");
                BindDataGrid();
            }
        }
        /// <summary>
        /// 停止用户
        /// </summary>
        protected void pause_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Users.PauseUsers(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        /// <summary>
        /// 开启用户
        /// </summary>
        protected void setuser_Click(object sender, EventArgs e)
        {
            int setuid = SASRequest.GetInt("ajaxcorpuserdatalist_value", 0);
            if (setuid > 0)
            {
                Users.UpdateUserFid(setuid, adminid);
                base.RegisterStartupScript("", "<script>alert('分配成功!');AjaxHelper.Updater('ajaxcorpuserlist_list.ascx', 'ajaxcorpuserdatalist', 'noinfid=" + adminid + "');</script>");
                BindDataGrid();
            }
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            string headname = "用户信息列表";

            groupid = Convert.ToInt16(ManageGroup.NormalGroup);
            parmStr = "(id <> manager or manager is null) AND gid = " + groupid +" AND fid = " + adminid;

            DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
            DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
            DataGrid1.AllowCustomPaging = true;
            DataGrid1.TableHeaderName = headname;
            DataGrid1.Attributes.Add("borderStyle", "0");
            DataGrid1.DataKeyField = "id";
            DataGrid1.ColumnSpan = 12;
            DataGrid1.SaveDSViewState = true;

            check.Click += new EventHandler(check_Click);
            //pause.Click += new EventHandler(pause_Click);
            setuser.Click += new EventHandler(setuser_Click);
            reuserpass.ServerClick += new EventHandler(reuserpass_ServerClick);
        }

        #endregion
    }
}